import { Engine } from "../engine";  
import { Script, ScriptAction, ScriptActionType } from "./script-interface";  
import { Vector2 } from "../vector";  
  
export class ScriptPlayer {  
  private engine: Engine;  
  private currentScript: Script | null = null;  
  private startTime: number = 0;  
  private isPlaying: boolean = false;  
  private actionIndex: number = 0;  
  private executedActions: Set<number> = new Set();  
  
  constructor(engine: Engine) {  
    this.engine = engine;  
  }  
  
  playScript(script: Script): void {  
    this.currentScript = script;  
    this.startTime = this.engine.time_;  
    this.isPlaying = true;  
    this.actionIndex = 0;  
    this.executedActions.clear();  
      
    console.log(`开始播放剧本: ${script.name}`);  
  }  
  
  stopScript(): void {  
    this.isPlaying = false;  
    this.currentScript = null;  
    this.executedActions.clear();  
      
    // 清理所有运镜效果  
    this.engine.camera.clearEffects();  
  }  
  
  update(): void {  
    if (!this.isPlaying || !this.currentScript) return;  
  
    const elapsed = this.engine.time_ - this.startTime;  
      
    // 检查是否有需要执行的动作  
    for (let i = 0; i < this.currentScript.actions.length; i++) {  
      const action = this.currentScript.actions[i];  
        
      if (!this.executedActions.has(i) && elapsed >= action.timestamp) {  
        this.executeAction(action);  
        this.executedActions.add(i);  
      }  
    }  
  
    // 检查剧本是否结束  
    if (elapsed >= this.currentScript.duration) {  
      this.stopScript();  
    }  
  }  
  
  private executeAction(action: ScriptAction): void {  
    switch (action.type) {  
      case ScriptActionType.DIALOGUE:  
        this.executeDialogue(action.data);  
        break;  
      case ScriptActionType.CAMERA_MOVE:  
        this.executeCameraMove(action.data);  
        break;  
      case ScriptActionType.CAMERA_ZOOM:  
        this.executeCameraZoom(action.data);  
        break;  
      case ScriptActionType.CAMERA_SHAKE:  
        this.executeCameraShake(action.data);  
        break;  
      case ScriptActionType.CAMERA_PATH:  
        this.executeCameraPath(action.data);  
        break;  
      case ScriptActionType.WAIT:  
        // 等待动作不需要特殊处理  
        break;  
      default:  
        console.warn(`未知的剧本动作类型: ${action.type}`);  
    }  
  }  
  
  private executeDialogue(data: any): void {  
    if (this.engine.player && this.engine.player.bubbleManager) {  
      this.engine.player.say(data.text, data.duration, data.bubbleOptions);  
    }  
  }  
  
  private executeCameraMove(data: any): void {  
    // 实现相机移动到指定位置  
    // 这需要扩展现有的相机系统  
  }  
  
  private executeCameraZoom(data: any): void {  
    if (data.focusOnPlayer) {  
      this.engine.camera.zoomToCharacter(data.targetZoom, data.duration);  
    } else {  
      this.engine.camera.zoomTo(data.targetZoom, data.duration);  
    }  
  }  
  
  private executeCameraShake(data: any): void {  
    this.engine.camera.shake(data.intensity, data.duration);  
  }  
  
  private executeCameraPath(data: any): void {  
    this.engine.camera.followPath(data.path, data.duration, data.loop);  
  }  
  
  get isScriptPlaying(): boolean {  
    return this.isPlaying;  
  }  
  
  get currentScriptName(): string | null {  
    if(!this.currentScript || !this.currentScript.name){
        return null;
    }
    return this.currentScript.name;  
  }  
}